/*
 * @Author: weidewei
 * @Date: 2025-11-01 14:36:25
 * @LastEditors: weidewei
 * @LastEditTime: 2025-11-01 15:22:02
 * @Description: 
 * @FilePath: \2025-front-face\js常见手写题\myApply.js
 */
function test(...params) {
    // console.log(this);
    console.log(this,params)
    return 123;
}

const p = {}
// test.apply(p,[1,2,3]);
/*
1.this指向确认：null或者undefined指向全局，否则指向传入的this的包装对象
2.避免属性冲突​
3.临时绑定 this
4.调用函数并传参​
5.清理临时属性​
6.返回结果​
*/
Function.prototype.myApply = function (ctx,args) {
    ctx = (ctx === null || ctx === undefined) ? globalThis : Object(ctx);
    const key = Symbol();
    Object.defineProperty(ctx,key,{
        enumerable: false,
        value: this
    })
    let res = ctx[key](...args);
    delete ctx[key];
    return res;
}
const res =  test.myApply(null,[1,2,3]);
console.log('rs',res)